package backend.db;

import java.sql.ResultSet;
import java.sql.SQLException;

import backend.Course;
import backend.Day;
import backend.Session;
import backend.User;

class Makers {
  static final Maker<Course> DEEP_COURSE_MAKER = new Maker<Course>() {
    @Override public Course make(final ResultSet r) throws SQLException {
      return makeCourseDeep(r);
    }
  };
  static final Maker<Course> SHALLOW_COURSE_MAKER = new Maker<Course>() {
    @Override public Course make(final ResultSet r) throws SQLException {
      return makeCourseShallow(r);
    }
  };
  static final Maker<Session> SESSION_MAKER = new Maker<Session>() {
    @Override public Session make(final ResultSet r) throws SQLException {
      return makeSession(r);
    }
  };
  static final Maker<String> COURSE_NAME_MAKER = new Maker<String>() {
    @Override public String make(final ResultSet r) throws SQLException {
      return r.getString("courseName");
    }
  };
  static final Maker<User> USER_MAKER = new Maker<User>() {
    @Override public User make(final ResultSet r) throws SQLException {
      return makeUser(r);
    }
  };
  
  static User makeUser(final ResultSet r) throws SQLException {
    return new User(r.getString("username"), r.getString("password"), r.getString("role"),
        r.getString("name"), r.getString("phone"), r.getString("address"));
  }
  
  static Course makeCourseDeep(final ResultSet r) throws SQLException {
    return makeCourse(r, true);
  }
  
  static Course makeCourseShallow(final ResultSet r) throws SQLException {
    return makeCourse(r, false);
  }
  
  private static Course makeCourse(final ResultSet r, final boolean deep) throws SQLException {
    final String courseId = r.getString("courseId");
    return new Course(r.getString("courseName"), courseId, r.getString("lecturerInCharge"),
        r.getString("moedA"), r.getString("moedB"), r.getString("description"), r.getString("creditPoints"),
        r.getInt("capacity"), deep ? ORM.getLectures(courseId) : null, deep ? ORM.getTutorials(courseId)
            : null, r.getInt("groupNum"), r.getString("owner"));
  }
  
  static Session makeSession(final ResultSet r) throws SQLException {
    return new Session(r.getString("courseId"), Day.values()[r.getInt("day")], r.getInt("startHour"),
        r.getInt("duration"), r.getString("teacherName"), r.getString("classLocation"),
        r.getBoolean("isLecture"));
  }
}
